<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="35%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="编号" prop="no">
      <el-input v-model="dataForm.no" placeholder="编号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="职务" prop="zwtype">
      <el-select
         v-model="dataForm.zwtype"
         multiple
         filterable
         default-first-option
         placeholder="职务">
         <el-option
           v-for="item in postList"
           :key="item.id"
           :label="item.label"
           :value="item.id">
         </el-option>
       </el-select>
    </el-form-item>
    <el-form-item label="关联操作用户" prop="operatorId">
      <el-select
         v-model="dataForm.operatorId"
         filterable
         placeholder="关联操作用户">
         <el-option
           v-for="item in sysList"
           :key="item.userId"
           :label="item.username"
           :value="item.userId">
         </el-option>
       </el-select>
    </el-form-item>
    <el-form-item label="联系电话" prop="lxtel">
      <el-input type="number" v-model="dataForm.lxtel"  placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phote">
      <el-input type="number" v-model="dataForm.phote" :maxlength="11" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remake">
      <el-input v-model="dataForm.remake" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
       <el-radio v-model="dataForm.status" label="0">无效</el-radio>
        <el-radio v-model="dataForm.status" label="1">有效</el-radio>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          no: '',
          name: '',
          zwtype: [],
          operatorId: '',
          lxtel: '',
          phote: '',
          address: '',
          remake: '',
          status: '',
          creatUserId: '',
          creatTime: '',
          sellerId: '',
        },
        sysList:[],
        postList:[],
        dataRule: {
          no: [
            { required: true, message: '编号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      zwgl() {
        this.$comReq.reqDictionaryData("post")
          .then(({
            data
          }) => {
            this.postList = data.page.list
          })
      },
      getDataList () {
       this.$comReq.reqMerchantOperator()
           .then(({
             data
           }) => {
             this.sysList = data.list
           })
       },
      init (id) {
        this.getDataList();
        this.zwgl();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tmarketingmanagement/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.no = data.tMarketingManagement.no
                this.dataForm.name = data.tMarketingManagement.name
                this.dataForm.zwtype = data.tMarketingManagement.zwtypes
                this.dataForm.operatorId = parseInt(data.tMarketingManagement.operatorId)
                this.dataForm.lxtel = data.tMarketingManagement.lxtel
                this.dataForm.phote = data.tMarketingManagement.phote
                this.dataForm.address = data.tMarketingManagement.address
                this.dataForm.remake = data.tMarketingManagement.remake
                this.dataForm.status = data.tMarketingManagement.status
                this.dataForm.creatUserId = data.tMarketingManagement.creatUserId
                this.dataForm.creatTime = data.tMarketingManagement.creatTime
                this.dataForm.sellerId = data.tMarketingManagement.sellerId
              }
            })
          }
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tmarketingmanagement/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'no': this.dataForm.no,
                'name': this.dataForm.name,
                'zwtypes': this.dataForm.zwtype,
                'operatorId': this.dataForm.operatorId,
                'lxtel': this.dataForm.lxtel,
                'phote': this.dataForm.phote,
                'address': this.dataForm.address,
                'remake': this.dataForm.remake,
                'status': this.dataForm.status,
                'creatUserId': this.dataForm.creatUserId,
                'creatTime': this.dataForm.creatTime,
                'sellerId': this.dataForm.sellerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
